package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    DishService dishService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    DishFlavorService dishFlavorService;

    /**
     * 保存菜品信息
     *
     * @param dishDto
     * @return
     */

    @PostMapping
    public R<String> saveDish(@RequestBody DishDto dishDto) {
        log.info(dishDto.toString());

        //自定义service保存菜品信息
        dishService.saveWithFlavor(dishDto);
        return R.success("保存成功");
    }


    /**
     * 菜品信息分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("page")
    public R<Page> findAll(int page, int pageSize, String name) {

        //分页构造器
        Page<Dish> pageInfo = new Page<>(page, pageSize);

        //条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Dish::getName, name);
        //排序构造器
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        dishService.page(pageInfo, queryWrapper);

        //DishDto 分页构造器
        Page<DishDto> dishDtoPage = new Page<>();

        // 价格pageInfo中除了 records的信息都拷贝到dishDtoPage中
        BeanUtils.copyProperties(pageInfo, dishDtoPage, "records");

        //获取pageInfo中的records信息  根据dish中的categoryId查找到categoryName信息 放入dishDto中
        List<DishDto> dishDtoList = pageInfo.getRecords().stream().map(dish -> {
            DishDto dishDto = new DishDto();
            //将dish中的信息拷贝到dishDto中
            BeanUtils.copyProperties(dish, dishDto);
            //获取dish转存到dishDto的categoryId
            Long categoryId = dishDto.getCategoryId();
            //根据categoryId查找category
            Category category = categoryService.getById(categoryId);
            //将categoryName存入DishDto中
            dishDto.setCategoryName(category.getName());
            return dishDto;
        }).collect(Collectors.toList());

        //将dishDtoList存入dishDtoPage的records中
        dishDtoPage.setRecords(dishDtoList);
        return R.success(dishDtoPage);
    }

    /**
     * 根据id查询菜品信息 用于数据回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> findByIdWithFlavors(@PathVariable Long id){
        //查询dish
        Dish dish = dishService.getById(id);
        DishDto dishDto=new DishDto();
        //将dish复制到dishDto
        BeanUtils.copyProperties(dish,dishDto);
        //根据dish_id查口味
        LambdaQueryWrapper<DishFlavor> flavorLambdaQueryWrapper =new LambdaQueryWrapper<>();
        flavorLambdaQueryWrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> list = dishFlavorService.list(flavorLambdaQueryWrapper);
        dishDto.setFlavors(list);
        return R.success(dishDto);
    }


    /**
     * 修改菜品信息
     * @param dishDto
     * @return
     */

    @PutMapping
    public R<String> upData(@RequestBody DishDto dishDto){

        dishService.upDataWithFlavors(dishDto);

        return R.success("修改成功");
    }

   /* *//**
     *
     * @param dish
     * @return
     *//*
    @GetMapping("/list")
    public R<List<Dish>> getList(Dish dish){

        LambdaQueryWrapper<Dish> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getCategoryId,dish.getCategoryId());
        lambdaQueryWrapper.orderByAsc(Dish::getUpdateTime);
        List<Dish> list = dishService.list(lambdaQueryWrapper);

        return R.success(list);
    }
*/


     /*
     *
     * @param dish
     * @return
    */
    @GetMapping("/list")
    public R<List<DishDto>> getList(Dish dish){

        LambdaQueryWrapper<Dish> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getCategoryId,dish.getCategoryId());
        lambdaQueryWrapper.orderByAsc(Dish::getUpdateTime);
        lambdaQueryWrapper.eq(Dish::getStatus,dish.getStatus());
        List<Dish> list = dishService.list(lambdaQueryWrapper);

        //根据dishId去口味表查找口味信息
        List<DishDto> dishDtoList = list.stream().map(new Function<Dish, DishDto>() {
            @Override
            public DishDto apply(Dish dish) {
                DishDto dishDto = new DishDto();
                BeanUtils.copyProperties(dish, dishDto);
                //根据菜品id查找口味信息
                LambdaQueryWrapper<DishFlavor> dishDtoLambdaQueryWrapper = new LambdaQueryWrapper<>();
                dishDtoLambdaQueryWrapper.eq(DishFlavor::getDishId, dish.getId());
                List<DishFlavor> dishFlavorList = dishFlavorService.list(dishDtoLambdaQueryWrapper);
                dishDto.setFlavors(dishFlavorList);
                return dishDto;
            }
        }).collect(Collectors.toList());

        return R.success(dishDtoList);
    }
}
